package com.qc31.monitorlib.login

import com.qc31.baselib.R
import com.qc31.baselib.net.AppContext
import com.qc31.baselib.rx.LiveDataBus
import com.qc31.baselib.utils.CacheMMKV
import com.qc31.baselib.utils.GsonUtil
import com.qc31.comlib.AppConfigs
import com.qc31.comlib.entity.GpsDetail
import com.qc31.comlib.entity.LocationStatus
import com.qc31.comlib.entity.NameValue
import com.qc31.comlib.map.AppGpsColumn

/** @Author pc_hfjs @Date 2022/1/13 17:24 @Desc */
object LocationStatusModel {
    private val switchs = ArrayList<AppGpsColumn>()
    private val switchList = ArrayList<LocationStatus>()
    val changeList = ArrayList<AppGpsColumn>()

    fun initData() {
        val local = CacheMMKV.decodeString("AppGpsColumn")
        val info = AppConfigs.appGpsColumn ?: if (local.isEmpty()) {
            val default =
                "[{\"key\":\"drctCn\",\"switch\":1},{\"key\":\"accTime\",\"switch\":1},{\"key\":\"teamName\",\"switch\":1},{\"key\":\"driverStatic\",\"switch\":0},{\"key\":\"driverSign\",\"switch\":0},{\"key\":\"alarms\",\"switch\":0},{\"key\":\"addr\",\"switch\":1},{\"key\":\"st1\",\"switch\":0},{\"key\":\"st3\",\"switch\":0},{\"key\":\"st4\",\"switch\":0},{\"key\":\"st5\",\"switch\":0},{\"key\":\"st2\",\"switch\":0},{\"key\":\"carName\",\"switch\":0},{\"key\":\"satl\",\"switch\":0},{\"key\":\"sgn\",\"switch\":0}]"
            GsonUtil.formArrayList(default, AppGpsColumn::class.java)
        } else {
            GsonUtil.formArrayList(local, AppGpsColumn::class.java)
        }
        switchs.clear()
        switchs.addAll(info.filter { it.switch == 1 })
        changeList.clear()
        changeList.addAll(info)

    }

    fun update(data: MutableList<AppGpsColumn>) {
        changeList.clear()
        changeList.addAll(data)
        switchs.clear()
        switchs.addAll(changeList.filter { it.switch == 1 })
    }


    fun saveLocal() {
        val toJson = GsonUtil.toJson(changeList)
        CacheMMKV.encode("AppGpsColumn", toJson)
        LiveDataBus.get().with("upInfoState").postValue(true)
    }

    /** 更新并返回数据 */

    private fun getString(resId: Int): String {
        return AppContext.getDescById(resId)
    }

    fun upData(data: GpsDetail?): ArrayList<LocationStatus> {
        switchList.clear()
        data?.run {
            switchs.forEach {
                val status = LocationStatus(it.key, it.switch)
                switchList.add(status)
                when (it.key) {
                    "drctCn" -> {
                        status.value = drctCn
                    }
                    "accTime" -> {
                        status.value = accTime
                    }
                    "teamName" -> {
                        status.value = teamName
                    }
                    "alarms" -> {
                        status.value = alarms ?: ""
                    }
                    "addr" -> {
                        status.value = addr ?: ""
                    }
                    "st1" -> {
                        status.value = getDesc(st1)
                    }
                    "st2" -> {
                        status.value = getDesc(st2)
                    }
                    "st3" -> {
                        status.value = getDesc(st3)
                    }
                    "st4" -> {
                        status.value = getDesc(st4)
                    }
                    "st5" -> {
                        status.value = getDesc(st5)
                    }
                    "carName" -> {
                        status.value = carName
                    }
                    "driverStatic" -> {
                        status.value =
                            if (drvName.isEmpty()) getString(R.string.desc_no_driver) else "$drvName:$drvPhone"
                    }
                    "satl" -> {
                        status.value = satl ?: ""
                    }
                    "sgn" -> {
                        status.value = sgn ?: ""
                    }

                }
            }

        }
        return switchList
    }

    private fun getDesc(st: List<NameValue>?): String {
        var sensor = ""
        st?.forEach {
            sensor = "$sensor,${it.name}:${it.value}"
        }
        if (sensor.length > 1)
            sensor = sensor.substring(1)

        return sensor
    }

    /** 通过key 获取描述 */

    fun getDescByKey(key: String): Int {
        return when (key) {
            "drctCn" -> R.string.desc_run_drct
            "accTime" -> R.string.desc_acc_state
            "teamName" -> R.string.desc_belong_team
            "driverStatic" -> R.string.desc_driver_static
            "driverSign" -> R.string.desc_driver_sign
            "alarms" -> R.string.desc_now_alarm
            "addr" -> R.string.desc_com_address
            "st1" -> R.string.desc_oil_info
            "st3" -> R.string.desc_temp_info
            "st4" -> R.string.desc_damp_info
            "st5" -> R.string.desc_water_info
            "st2" -> R.string.desc_wight_info
            "carName" -> R.string.desc_offline_car_name
            "satl" -> R.string.desc_map_info_satl
            "sgn" -> R.string.desc_map_info_sgn
            else -> 0
        }
    }
}